package sdmakerz_v1.coresyntax.generator

import java.util.ArrayList
import java.util.List
import org.eclipse.emf.ecore.EObject
import sdmakerz_v1.coresyntax.sDMakerV1.Obj
import sdmakerz_v1.coresyntax.sDMakerV1.Call
import sdmakerz_v1.coresyntax.sDMakerV1.Diagram
import sdmakerz_v1.coresyntax.generator.interfaces.IInterfaceData
import sdmakerz_v1.coresyntax.generator.interfaces.IMethodData
import sdmakerz_v1.coresyntax.generator.interfaces.IInputData
import sdmakerz_v1.coresyntax.SDMakerV1Utils

/**
 * Une classe qui simplfie la recuperation des donnees d'une Interface
 * et de ses instances.
 * @author pstl-team
 */
class InterfaceDataImpl implements IInterfaceData {
    /** Le nom de l'interface */
    private String name;
    
    /** L'identifiant unique de l'ObjectClass representant cette
     *  interface */
    private String interfaceID;
    
    /* les noms des instances existantes du type */
    private List<Obj> objs;

	private ArrayList<IMethodData> methodsDatas;
    
    
    
    /* ****************** *
     * ** INITIALIZERS ** *
     * ****************** */   
     
    override void init(){
    	objs= new ArrayList<Obj>();
    	methodsDatas= new ArrayList<IMethodData>();
    }
    
    
    
    /* ************* *
     * ** SETTERS ** *
     * ************* */
     
    override IInterfaceData setName(String name){
        this.name= name;
        return this;
    }
    
    override void setInterfaceID(String interfaceID){
        this.interfaceID= interfaceID;
    }
    
    override void addObj(Obj obj){
    	objs.add(obj);
    }
    
    override void addCall(IInputData inputData, Call c){
    	
    	var List<IMethodData> matchingMethods= matchingMethods(inputData, c);
    	
    	if(matchingMethods.size==0){
    		var IMethodData methodData= new MethodDataImpl();
    		
    		methodData.init(inputData, c);
    		
    		methodData.addCall(inputData, c);
    		methodsDatas.add(methodData);
    		matchingMethods.add(methodData);
    	}

    	for(IMethodData methodData : matchingMethods){
    			methodData.addCall(inputData, c);
    	}	
    }



    /* ************* *
     * ** GETTERS ** *
     * ************* */
   	
   	override String getInterfaceID(){
        return interfaceID;
    }
    
    override String getQualifiedName(){
        return name;
    }
    
    override IMethodData getMethodData(IInputData inputData, Call c){
    	var IMethodData methodData= null;
    	var List<IMethodData> matchingMethod= matchingMethods(inputData, c);
    	
    	// != 1 veut dire non trouve ou conflit
    	if(matchingMethod.size == 1){
    		methodData= matchingMethod.get(0);
    	}
    	
    	return methodData;
    }
    
    override List<IMethodData> getAllMethodData(){
    	return methodsDatas;
    }
    
    def private List<IMethodData> matchingMethods(IInputData inputData, Call c){
    	 var List<IMethodData> matchingMethod= new ArrayList<IMethodData>();
    	 
    	for(IMethodData methodData : methodsDatas){
    		if(methodData.match(inputData, c)){
    			matchingMethod.add(methodData);
    		}
    	}
    	
    	return matchingMethod
    } 



    /* ************* *
     * ** TESTORS ** *
     * ************* */
    
    override Boolean contains(Obj obj){
        var contains= false;
        
        for(Obj objTmp : objs){
        	var String strURI= obj.eResource.URI.toString;
        	var String strURITmp= objTmp.eResource.URI.toString;
        	var EObject eo= SDMakerV1Utils::findContainer(obj, typeof(Diagram));
        	var EObject eoTmp= SDMakerV1Utils::findContainer(objTmp, typeof(Diagram));
        	var Diagram d= eo as Diagram;
        	var dTmp= eoTmp as Diagram;
        	
        	if(strURI!=null && strURITmp != null && 
               strURI.compareTo(strURITmp)==0){
               	
        		if(d!=null && dTmp!=null && 
        		   d.name.compareTo(dTmp.name)==0){
        		
        			contains= (obj.name.compareTo(objTmp.name)==0);
        		}
        	}
        }
        
        return contains;
    }
}